package com.cinsea.mobile.ext;

import com.alibaba.fastjson.JSON;
import com.cinsea.common.*;
import com.cinsea.hrm.service.HumanService;
import com.cinsea.htmlselect.service.OptionService;
import com.cinsea.interfaces.form.Formdata;
import com.cinsea.interfaces.form.FormdataServiceImpl;
import com.cinsea.interfaces.model.Cell;
import com.cinsea.interfaces.model.Dataset;
import com.cinsea.interfaces.workflow.RequestInfo;
import com.cinsea.interfaces.workflow.WorkflowServiceImpl;
import com.cinsea.msg.Message;
import com.cinsea.msg.MessageProducer;
import com.cinsea.security.service.RightService;
import com.cinsea.security.util.JdbcUtil;
import com.cinsea.util.DateUtil;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: xf
 * Date: 13-7-21
 * Time: 下午5:04
 */
public class XycAction extends ExtAction {
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String action = StringUtil.null2String(request.getParameter("action"));
        String key = StringUtil.null2String(request.getParameter("q"));
        int page = NumberUtil.getIntegerValue(request.getParameter("page"), 1);
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        HumanService humanService = Env.getBean("humanService");
        if (action.equals("getmenu")) {
            Map menu1 = new HashMap();
            menu1.put("text", "养护管理");
            ArrayList menu1childs = new ArrayList();
            menu1.put("items", menu1childs);

            Map menu11 = new HashMap();
            menu11.put("text", "换花");
            menu11.put("leaf", true);
            Map menu12 = new HashMap();
            menu12.put("text", "换花车");
            menu12.put("leaf", true);
            Map menu13 = new HashMap();
            menu13.put("text", "签收单");
            menu13.put("leaf", true);
            menu1childs.add(menu11);
            menu1childs.add(menu12);
            menu1childs.add(menu13);
            Map menu2 = new HashMap();
            menu2.put("text", "采购管理");
            ArrayList menu2childs = new ArrayList();
            menu2.put("items", menu2childs);
            Map menu21 = new HashMap();
            menu21.put("text", "待采购清单");
            menu21.put("leaf", true);
            Map menu22 = new HashMap();
            menu22.put("text", "采购汇总表");
            menu22.put("leaf", true);
            Map menu23 = new HashMap();
            menu23.put("text", "送货确认单");
            ArrayList menu21childs = new ArrayList();
            menu23.put("items",menu21childs);
            Map menu231 = new HashMap();
            menu231.put("text", "换花确认单");
            menu231.put("leaf", true);
            Map menu232 = new HashMap();
            menu232.put("text", "赠/售花确认单");
            menu232.put("leaf", true);
            menu21childs.add(menu231);
            menu21childs.add(menu232);
            menu2childs.add(menu21);
            menu2childs.add(menu22);
            menu2childs.add(menu23);
            Map root = new HashMap();
            ArrayList rootchilds = new ArrayList();
            rootchilds.add(menu1);
            rootchilds.add(menu2);
            root.put("items", rootchilds);
            response.getWriter().print(JSON.toJSONString(root));
        }
        if (action.equals("getplace1list")) {
            StringBuffer sql = new StringBuffer("select id,lcmc,bianma,yhry from ut_floorinfo where  leixing='402880333fa84910013fa87395f000b4' and deleted=0 and 1=1 ");
            ArrayList sqlparam = new ArrayList();
            if (!StringUtil.isEmpty(key)) {
                sql.append(" and lcmc like ?");
                sqlparam.add("%" + key + "%");
            }
            sql.append(" order by modifydatetime desc ");
            String[] _sql = JdbcUtil.getPermissionSql(sql.toString(), "ut_floorinfo", sqlparam);
            Page pageObject = jdbcDao.pagedQuery(_sql[0], StringUtil.string2Array(_sql[1], ","), page, 25);
            if (pageObject.getTotalSize() != 0) {
                List<Map> result = (List) pageObject.getResult();
                List jsonlist = new ArrayList();
                for (Map obj : result) {
                    Map rowdata = new HashMap();
                    rowdata.put("id", obj.get("id"));
                    rowdata.put("processid", obj.get("id"));
                    rowdata.put("title", obj.get("lcmc"));
                    String managerName = humanService.getHumanNamesByIds(obj.get("yhry").toString());
                    rowdata.put("yhry", managerName);
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("totalCount", pageObject.getTotalSize());
                object.put("result", jsonlist);
                gzip(request, response, JSON.toJSONString(object));
            } else {
                Map object = new HashMap();
                object.put("totalCount", 0);
                response.getWriter().print(JSON.toJSONString(object));
            }
        }
        if (action.equals("getplace2list")) {//摆位列表
            StringBuffer sql = new StringBuffer("select id,lcmc,bianma,yhry from ut_floorinfo where  suoshulouyu=? and deleted=0 and 1=1 ");
            ArrayList sqlparam = new ArrayList();
            String pid = StringUtil.null2String(request.getParameter("pid"));
            sqlparam.add(pid);
            if (!StringUtil.isEmpty("key")) {
                sql.append(" and lcmc like ?");
                sqlparam.add("%" + key + "%");
            }
            sql.append(" order by modifydatetime desc ");
            String[] _sql = JdbcUtil.getPermissionSql(sql.toString(), "ut_floorinfo", sqlparam);
            Page pageObject = jdbcDao.pagedQuery(_sql[0], StringUtil.string2Array(_sql[1], ","), page, 25);
            if (pageObject.getTotalSize() != 0) {
                List<Map> result = (List) pageObject.getResult();
                List jsonlist = new ArrayList();
                for (Map obj : result) {
                    Map rowdata = new HashMap();
                    rowdata.put("id", obj.get("id"));
                    rowdata.put("processid", obj.get("id"));
                    rowdata.put("title", obj.get("lcmc"));
                    String managerName = humanService.getHumanNamesByIds(obj.get("yhry").toString());
                    rowdata.put("yhry", managerName);
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("totalCount", pageObject.getTotalSize());
                object.put("result", jsonlist);
                gzip(request, response, JSON.toJSONString(object));
            } else {
                Map object = new HashMap();
                object.put("totalCount", 0);
                response.getWriter().print(JSON.toJSONString(object));
            }
        }
        if (action.equals("getplace3list")) {//绿植列表
            StringBuffer sql = new StringBuffer("select id,lvzhi,huapen,huajia,bfsl,hpsl,hjsl,bwsm from ut_postplant where  baiwei=? and deleted=0 and 1=1 ");
            ArrayList sqlparam = new ArrayList();
            String pid = StringUtil.null2String(request.getParameter("pid"));
            sqlparam.add(pid);
            sql.append(" order by modifydatetime desc ");
            List<Map> result = jdbcDao.queryForList(sql.toString(), sqlparam.toArray());
            List jsonlist = new ArrayList();
            for (Map obj : result) {
                String plantsql="select chmc,cunhuobianma,(select objname from cs_option where id=ut_plantinfo.chdlmc) as chdlmc,(select objname from cs_option where id=ut_plantinfo.guigexinghao) as guigexinghao from ut_plantinfo where id=?";
                if (!StringUtil.null2String(obj.get("lvzhi")).equals("")&&NumberUtil.getIntegerValue(obj.get("bfsl"))>0) {
                    List<Map> plantinfo = jdbcDao.queryForList(plantsql, new Object[]{obj.get("lvzhi")});
                    if (plantinfo.size() > 0) {
                        Map rowdata = new HashMap();
                        rowdata.put("id", IDGenerator.getUnquieID());
                        rowdata.put("plantid", obj.get("lvzhi"));
                        Map plant = plantinfo.get(0);
                        rowdata.put("title", plant.get("chmc"));
                        rowdata.put("code", obj.get("bwsm"));
                        rowdata.put("type1", plant.get("chdlmc"));
                        rowdata.put("type2", plant.get("guigexinghao"));
                        rowdata.put("amount", obj.get("bfsl"));
                        rowdata.put("processid", obj.get("id"));
                        jsonlist.add(rowdata);
                    }
                }
                if (!StringUtil.null2String(obj.get("huapen")).equals("")&&NumberUtil.getIntegerValue(obj.get("hpsl"))>0) {
                    List<Map> plantinfo = jdbcDao.queryForList(plantsql, new Object[]{obj.get("huapen")});
                    if (plantinfo.size() > 0) {
                        Map rowdata = new HashMap();
                        rowdata.put("id", IDGenerator.getUnquieID());
                        rowdata.put("plantid", obj.get("huapen"));
                        Map plant = plantinfo.get(0);
                        rowdata.put("title", plant.get("chmc"));
                        rowdata.put("code", obj.get("bwsm"));
                        rowdata.put("type1", plant.get("chdlmc"));
                        rowdata.put("type2", plant.get("guigexinghao"));
                        rowdata.put("amount", obj.get("hpsl"));
                        rowdata.put("processid", obj.get("id"));
                        jsonlist.add(rowdata);
                    }
                }
                if (!StringUtil.null2String(obj.get("huajia")).equals("")&&NumberUtil.getIntegerValue(obj.get("hjsl"))>0) {
                    List<Map> plantinfo = jdbcDao.queryForList(plantsql, new Object[]{obj.get("huajia")});
                    if (plantinfo.size() > 0) {
                        Map rowdata = new HashMap();
                        rowdata.put("id", IDGenerator.getUnquieID());
                        rowdata.put("plantid", obj.get("huajia"));
                        Map plant = plantinfo.get(0);
                        rowdata.put("title", plant.get("chmc"));
                        rowdata.put("code", obj.get("bwsm"));
                        rowdata.put("type1", plant.get("chdlmc"));
                        rowdata.put("type2", plant.get("guigexinghao"));
                        rowdata.put("amount", obj.get("hjsl"));
                        rowdata.put("processid", obj.get("id"));
                        jsonlist.add(rowdata);
                    }
                }

            }
            Map object = new HashMap();
            object.put("result", jsonlist);
            gzip(request, response, JSON.toJSONString(object));

        }

        if (action.equals("oldchangeflower")) {//提交换花流程（废弃，需求变更为提交表表）。
            String num = StringUtil.null2String(request.getParameter("num"));
            String processid = StringUtil.null2String(request.getParameter("processid"));
            List<Map> postplantdata = jdbcDao.queryForList("select baiwei,lvzhi from ut_postplant where processid=?", new Object[]{processid});
            if (postplantdata.size() > 0) {
                Map postplant = postplantdata.get(0);
                String baiwei = StringUtil.null2String(postplant.get("baiwei"));
                String lvzhi = StringUtil.null2String(postplant.get("lvzhi"));
                String apper = Env.getRemoteUser().getId();
                String appdate = DateUtil.getCurrentDate();
                String apptime = DateUtil.getCurrentTime();
                WorkflowServiceImpl si = new WorkflowServiceImpl();
                RequestInfo formdata = new RequestInfo();
                formdata.setOperator(Env.getRemoteUser().getId());
                //formdata.setTitle();
                formdata.setTypeid("4028818b402812110140295f7fbc0022"); //换花申请流程id
                String crmid = "";
                Dataset ds = new Dataset();
                List<Cell> mainlist = new ArrayList(); //主表单元格
                Cell c1 = new Cell();
                c1.setId("ut_changeplant_baiwei");
                c1.setValue(baiwei);
                Cell c2 = new Cell();
                c2.setId("ut_changeplant_lvzhi");
                c2.setValue(lvzhi);
                Cell c3 = new Cell();
                c3.setId("ut_changeplant_hhsl");
                c3.setValue(num);

                Cell c4 = new Cell();
                c4.setId("ut_changeplant_applier");
                c4.setValue(apper);

                Cell c5 = new Cell();
                c5.setId("ut_changeplant_appdate");
                c5.setValue(appdate);

                Cell c6 = new Cell();
                c6.setId("ut_changeplant_apptime");
                c6.setValue(apptime);
                mainlist.add(c1);
                mainlist.add(c2);
                mainlist.add(c3);
                mainlist.add(c4);
                mainlist.add(c5);
                mainlist.add(c6);
                ds.setMaintable(mainlist);
                formdata.setData(ds);
                si.createRequest(formdata);
            }
            response.getWriter().print("{\"success\":true}");
        }
        if (action.equals("changeflower")) {
            String processid = StringUtil.null2String(request.getParameter("processid"));
            String lzid = StringUtil.null2String(request.getParameter("lzid"));
            String lzsl = StringUtil.null2String(request.getParameter("lzsl"));
            String hhsl = StringUtil.null2String(request.getParameter("hhsl"));
            String hhbz = StringUtil.null2String(request.getParameter("hhbz"));
            String type = StringUtil.null2String(request.getParameter("type"));

            List cart = jdbcDao.queryForList("select id from ut_changeplant where deleted=0 and bwlzid=? and lvzhi=? and hhzt='402881934061fddd0140622f80720008'", new Object[]{processid,lzid});
            if (cart.size() > 0) {
                    response.getWriter().print("换花车中已存在该绿植");
                    return;
            }

            OptionService optionService = Env.getBean("optionService");
            List<Map> postplantdata = jdbcDao.queryForList("select baiwei,lvzhi from ut_postplant where processid=?", new Object[]{processid});
            if (postplantdata.size() > 0) {
                Map postplant = postplantdata.get(0);
                String baiwei = StringUtil.null2String(postplant.get("baiwei"));
                String apper = Env.getRemoteUser().getId();
                String appdate = DateUtil.getCurrentDate();
                String apptime = DateUtil.getCurrentTime();
                FormdataServiceImpl si = new FormdataServiceImpl();
                Formdata formdata = new RequestInfo();
                formdata.setOperator(Env.getRemoteUser().getId());
                formdata.setTypeid("402881934060ce6c01406194917b000d"); //换花申请目录id
                String crmid = "";
                Dataset ds = new Dataset();
                List<Cell> mainlist = new ArrayList(); //主表单元格
                Cell c1 = new Cell();
                c1.setId("ut_changeplant_baiwei");
                c1.setValue(baiwei);
                Cell c2 = new Cell();
                c2.setId("ut_changeplant_lvzhi");
                c2.setValue(lzid);
                Cell c3 = new Cell();


                Cell c4 = new Cell();
                c4.setId("ut_changeplant_applier");
                c4.setValue(apper);

                Cell c5 = new Cell();
                c5.setId("ut_changeplant_appdate");
                c5.setValue(appdate);

                Cell c6 = new Cell();
                c6.setId("ut_changeplant_apptime");
                c6.setValue(apptime);
                if (type.equals("1")) type = "402881934060ce6c014060ee39760005";//报损
                else if (type.equals("2")) type = "402881934060ce6c014060ee63250006";//代养
                else if (type.equals("3")) type = "402881934060ce6c014060ee80ab0007";//基地
                
                else {
                    response.getWriter().print("无效类型");
                    return;
                }
                Cell c7 = new Cell();
                c7.setId("ut_changeplant_hhlx");
                c7.setValue(type);

                Cell c8 = new Cell();
                c8.setId("ut_changeplant_hhzt");
                c8.setValue("402881934061fddd0140622f80720008");//换花车
                Cell c9 = new Cell();
                c9.setId("ut_changeplant_bwlzid");
                c9.setValue(processid);
                Map baiweidata = jdbcDao.queryForMap("select lcmc from ut_floorinfo where processid=?", new Object[]{baiwei});
                Cell c10 = new Cell();
                c10.setId("ut_changeplant_bwmc");
                c10.setValue(StringUtil.null2String(baiweidata.get("lcmc")));

                Map lvzhidata = jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc from ut_plantinfo where processid=?", new Object[]{lzid});
                Cell c12 = new Cell();
                c12.setId("ut_changeplant_guigexinghao");
                c12.setValue(StringUtil.null2String(optionService.get(StringUtil.null2String(lvzhidata.get("guigexinghao"))).getObjname()));
                Cell c13 = new Cell();
                c13.setId("ut_changeplant_lzmc");
                c13.setValue(StringUtil.null2String(lvzhidata.get("chmc")));
                Cell c14 = new Cell();
                c14.setId("ut_changeplant_lzlx");
                c14.setValue(StringUtil.null2String(optionService.get(StringUtil.null2String(lvzhidata.get("chdlmc"))).getObjname()));

                Cell c15 = new Cell();
                c15.setId("ut_changeplant_lzsl");
                c15.setValue(lzsl);
                Cell c16 = new Cell();
                c16.setId("ut_changeplant_hhsl");
                c16.setValue(hhsl);
                Cell c17 = new Cell();
                c17.setId("ut_changeplant_hhbz");
                c17.setValue(hhbz);
                mainlist.add(c1);
                mainlist.add(c2);
                mainlist.add(c3);
                mainlist.add(c4);
                mainlist.add(c5);
                mainlist.add(c6);
                mainlist.add(c7);
                mainlist.add(c8);
                mainlist.add(c9);
                mainlist.add(c10);
                mainlist.add(c12);
                mainlist.add(c13);
                mainlist.add(c14);
                mainlist.add(c15);
                mainlist.add(c16);
                mainlist.add(c17);
                ds.setMaintable(mainlist);
                formdata.setData(ds);
                si.createFormdata(formdata);
            }
            response.getWriter().print("success");
        }
        if (action.equals("getchangelist") || action.equals("getacceptlist")|| action.equals("getpurchaselist")) {
            String hhzt = "";
            if (action.equals("getchangelist")) hhzt = "402881934061fddd0140622f80720008";//换花车
            else if (action.equals("getacceptlist"))
            {
                hhzt = "402881934061fddd014062306129000b";//已送货完成
            }
            else if (action.equals("getpurchaselist")) hhzt = "402881934061fddd0140622fe59a000a";//已分配采购
            StringBuffer sql = new StringBuffer("select id,processid,bwlzid,baiwei,lvzhi,hhlx,caigoujine,lzsl,hhsl,hhbz,yzdj from ut_changeplant where  hhzt='" + hhzt + "'and applier=?  and deleted=0 ");
            if (action.equals("getpurchaselist"))
                sql = new StringBuffer("select id,processid,bwlzid,baiwei,lvzhi,hhlx,shifoucaigou,caigoujine,lzsl,hhsl,hhbz from ut_changeplant where  hhzt='" + hhzt + "' and caigoujine is null and cgry=? and deleted=0 ");
            sql.append(" order by modifydatetime desc ");
            OptionService optionService = Env.getBean("optionService");
            Page pageObject = jdbcDao.pagedQuery(sql.toString(), new Object[]{Env.getRemoteUser().getId()}, page, 25);
            if (pageObject.getTotalSize() != 0) {
                List<Map> result = (List) pageObject.getResult();
                List jsonlist = new ArrayList();
                for (Map obj : result) {
                    Map rowdata = new HashMap();
                    rowdata.put("id", obj.get("id"));
                    rowdata.put("processid", obj.get("id"));
                    rowdata.put("bwlzid", obj.get("bwlzid"));
                    rowdata.put("amount", obj.get("lzsl"));
                    rowdata.put("yzdj", obj.get("yzdj"));
                    rowdata.put("hhbz", obj.get("hhbz"));
                    rowdata.put("num", obj.get("hhsl"));
                    String baiwei = StringUtil.null2String(obj.get("baiwei"));
                    rowdata.put("baiwei", baiwei);
                    String lvzhi = StringUtil.null2String(obj.get("lvzhi"));
                    String hhlx = StringUtil.null2String(obj.get("hhlx"));
                    rowdata.put("lvzhi", lvzhi);
                    String shifoucaigou = StringUtil.null2String(obj.get("shifoucaigou"));
                    shifoucaigou = optionService.getOptionNamesByIds(shifoucaigou);
                    rowdata.put("caigou", shifoucaigou);
                    rowdata.put("hhlxbm", hhlx);
                    if (hhlx.equals("402881934060ce6c014060ee39760005")) hhlx = "报损";
                    else if (hhlx.equals("402881934060ce6c014060ee63250006")) hhlx = "代养";
                    else if (hhlx.equals("402881934060ce6c014060ee80ab0007")) hhlx = "基地";
                    else if (hhlx.equals("402881ee4278282501427983d20904c3")) hhlx = "加花";
                    else if (hhlx.equals("402881ee42b153ca0142da4e563313a9")) hhlx = "减花";
                    else if (hhlx.equals("402881ee4278282501427985399104c4")) hhlx = "赠花";
                    else if (hhlx.equals("402881ee4278282501427985787404c5")) hhlx = "售花";
                    else if (hhlx.equals("402881ee43226cb201435637c4f131ea")) hhlx = "调花";
                    else hhlx = "错误类型";
                    rowdata.put("type", hhlx);
                    String place1 = "";
                    String place2 = "";
                    String place3 = "";
                    if(!(lvzhi.equals(""))){
                    try {
                        Map place3map = (Map) jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc,xianjia from ut_plantinfo where processid=?", new Object[]{lvzhi});
                        place3 += StringUtil.null2String(place3map.get("chmc"));
                        if (action.equals("getpurchaselist")) {
                            String xianjia = StringUtil.null2String(place3map.get("xianjia"),"");
                            rowdata.put("maxprice", xianjia);
                        }
                        Map place3map1 = (Map) jdbcDao.queryForMap("select bwsm from ut_postplant where processid=?", new Object[]{obj.get("bwlzid")});
                        if (!action.equals("getpurchaselist"))
                        place3 += " " + StringUtil.null2String(place3map1.get("bwsm"));
                        place3 += " " + StringUtil.null2String(optionService.get(StringUtil.null2String(place3map.get("chdlmc"))).getObjname());
                        place3 += " " + StringUtil.null2String(optionService.get(StringUtil.null2String(place3map.get("guigexinghao"))).getObjname());

                    } catch (Exception e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    }
                    rowdata.put("place3", place3);
                    try {
                        Map place2map = (Map) jdbcDao.queryForMap("select suoshulouyu,lcmc from ut_floorinfo where processid=?", new Object[]{baiwei});
                        place2 = StringUtil.null2String(place2map.get("lcmc"));
                        String suoshulouyu = StringUtil.null2String(place2map.get("suoshulouyu"));
                        Map place1map = (Map) jdbcDao.queryForMap("select lcmc from ut_floorinfo where processid=?", new Object[]{suoshulouyu});
                        place1 = StringUtil.null2String(place1map.get("lcmc"));
                    } catch (Exception e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    rowdata.put("place2", place2);
                    rowdata.put("place1", place1);

                    if (action.equals("getpurchaselist")){
                        String price=StringUtil.null2String(obj.get("caigoujine"));
                        rowdata.put("price", price);
                    }
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("totalCount", pageObject.getTotalSize());
                object.put("result", jsonlist);
                gzip(request, response, JSON.toJSONString(object));
            } else {
                Map object = new HashMap();
                object.put("totalCount", 0);
                response.getWriter().print(JSON.toJSONString(object));
            }
        }
        if (action.equals("getpurchasesum")) {
            String hhzt = "402881934061fddd0140622fe59a000a";//已分配采购

            StringBuffer sql = new StringBuffer("select lvzhi,sum(caigouzonge) as caigouzonge,sum(hhsl) as hhsl from ut_changeplant where  hhzt='" + hhzt + "' and cgry=? and deleted=0 group by lvzhi");
            OptionService optionService = Env.getBean("optionService");
            List<Map> result = jdbcDao.queryForList(sql.toString(), new Object[]{Env.getRemoteUser().getId()});
            List jsonlist = new ArrayList();
            sql = new StringBuffer("select sum(caigouzonge) as caigouzonge from ut_changeplant where  hhzt='" + hhzt + "' and cgry=? and deleted=0 ");
            Float sum = (Float) jdbcDao.queryForObject(sql.toString(), new Object[]{Env.getRemoteUser().getId()}, Float.class);
            Map rowdata = new HashMap();
            rowdata.put("id", "0");
            rowdata.put("processid", "0");
            rowdata.put("place3", "总计");
            rowdata.put("total", sum);
            jsonlist.add(rowdata);
            if (result.size() != 0) {

                for (Map obj : result) {
                    rowdata = new HashMap();
                    rowdata.put("id", obj.get("lvzhi"));
                    rowdata.put("processid", obj.get("lvzhi"));
                    rowdata.put("total", "合计-"+obj.get("caigouzonge"));
                    rowdata.put("num","数量-"+obj.get("hhsl"));
                    String lvzhi = StringUtil.null2String(obj.get("lvzhi"));

                    String place1 = "";
                    String place2 = "";
                    String place3 = "";
                    try {
                        Map place3map = (Map) jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc,xianjia from ut_plantinfo where processid=?", new Object[]{lvzhi});
                        place3 += StringUtil.null2String(place3map.get("chmc"));
                        place3 += " " + StringUtil.null2String(optionService.get(StringUtil.null2String(place3map.get("chdlmc"))).getObjname());
                        place3 += " " + StringUtil.null2String(optionService.get(StringUtil.null2String(place3map.get("guigexinghao"))).getObjname());

                    } catch (Exception e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    rowdata.put("place3", place3);
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("result", jsonlist);
                gzip(request, response, JSON.toJSONString(object));
            } else {
                Map object = new HashMap();
                object.put("totalCount", 0);
                response.getWriter().print(JSON.toJSONString(object));
            }
        }
        if (action.equals("cancelchange")) {
            String processid = StringUtil.null2String(request.getParameter("processid"));
            jdbcDao.update("update cs_formprocess set deleted=1 where id=?", new Object[]{processid});
            jdbcDao.update("update ut_changeplant set deleted=1 where processid=?", new Object[]{processid});
            response.getWriter().print("success");

        }
        if (action.equals("updatechange")) {
            String processid = StringUtil.null2String(request.getParameter("processid"));
            String hhsl = StringUtil.null2String(request.getParameter("hhsl"));
            jdbcDao.update("update ut_changeplant set hhsl=? where processid=?", new Object[]{hhsl,processid});
            response.getWriter().print("success");

        }
        if (action.equals("okchange")) {
            //已申请换花
            RightService rightService=Env.getBean("rightService");
            List<Map> changelist=jdbcDao.queryForList("select processid,hhlx,hhsl,lzsl,bwlzid,lvzhi from ut_changeplant where hhzt='402881934061fddd0140622f80720008' and applier=? and deleted=0", new Object[]{Env.getRemoteUser().getId()});
            for(Map change:changelist){
                String processid = StringUtil.null2String(change.get("processid"));
                jdbcDao.update("update ut_changeplant set  hhzt='402881934061fddd0140622fbc2a0009' where processid=?", new Object[]{processid});
                
                rightService.reCreateRightdetail(processid,"0","ut_changeplant");
            }
            Message m = new Message();
            m.setMsgtype(Message.MESSAGE_TYPE_EMAIL);
            m.setMsgtitle("换花申请");
            m.setMsgcontent("您好！您新增了" + changelist.size() + "条换花申请需要处理");
            m.setReceivers("402881e640afd7f70140afe7584e009b");
            m.setSender(Env.getRemoteUser().getId());
            MessageProducer msgpro = Env.getBean("messageProducer");
            msgpro.send(m);
            response.getWriter().print("success");

        }
        if (action.equals("acceptchange")||action.equals("rejectchange")) {
            String hhzt = "";
            if (action.equals("acceptchange"))
                hhzt = "402881934061fddd014062308165000c";//已验收确认
            else if (action.equals("rejectchange")) hhzt = "402881934061fddd01406230d3b9000d";//拒收
            String processid = StringUtil.null2String(request.getParameter("processid"));//已申请换花
            jdbcDao.update("update ut_changeplant set  hhzt='"+hhzt+"' where processid=?", new Object[]{processid});
            response.getWriter().print("success");

        }
        if (action.equals("purchase") ) {
            String processid = StringUtil.null2String(request.getParameter("processid"));//采购定价
            String price=StringUtil.null2String(request.getParameter("price"));
            if(price.equals("")){
                price = "0";
            }
            String modifydatetime=DateUtil.getCurDateTime();
            jdbcDao.update("update ut_changeplant set  modifydatetime=?,caigoujine=?,caigouriqi=?,caigouzonge="+price+"*hhsl where processid=?", new Object[]{modifydatetime,price,DateUtil.getCurrentDate(),processid});//已送货完成
            response.getWriter().print("{\"success\":true}");

        }
        if (action.equals("sendplacelist")) {
            StringBuffer sql = new StringBuffer("select id,lcmc,bianma,yhry from ut_floorinfo where  deleted=0 and id in( ");
            sql.append("select distinct suoshulouyu from ut_floorinfo where deleted=0 and id in(");
            sql.append("select baiwei from ut_changeplant where  hhzt='402881934061fddd0140622fe59a000a' and cgry=? and deleted=0 ");
            sql.append(") ");
            sql.append(") ");

            List<Map> placelist = jdbcDao.queryForList(sql.toString(), new Object[]{Env.getRemoteUser().getId()});
            if (placelist.size() != 0) {
                List jsonlist = new ArrayList();
                for (Map obj : placelist) {
                    Map rowdata = new HashMap();
                    rowdata.put("id", obj.get("id"));
                    rowdata.put("processid", obj.get("id"));
                    rowdata.put("title", obj.get("lcmc"));
                    String managerName = humanService.getHumanNamesByIds(obj.get("yhry").toString());
                    rowdata.put("yhry", managerName);
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("result", jsonlist);
                gzip(request, response, JSON.toJSONString(object));
            } else {
                Map object = new HashMap();
                response.getWriter().print(JSON.toJSONString(object));
            }
        }
        if (action.equals("getsendlist")) {
            OptionService optionService = Env.getBean("optionService");
            StringBuffer sql = new StringBuffer("select id,processid,bwlzid,baiwei,lvzhi,hhlx,caigoujine,hhsl,caigouzonge from ut_changeplant where  hhzt='402881934061fddd0140622fe59a000a' and cgry=? and deleted=0 ");
            sql.append("and caigoujine is not null and baiwei in(select id from ut_floorinfo where deleted=0 and suoshulouyu=?)");
            String louyuid = StringUtil.null2String(request.getParameter("louyuid"));

            List<Map> sendlist = jdbcDao.queryForList(sql.toString(), new Object[]{Env.getRemoteUser().getId(), louyuid});
            if (sendlist.size() != 0) {
                List jsonlist = new ArrayList();
                for (Map obj : sendlist) {
                    Map rowdata = new HashMap();
                    rowdata.put("id", obj.get("id"));
                    rowdata.put("processid", obj.get("id"));
                    rowdata.put("bwlzid", obj.get("bwlzid"));
                    rowdata.put("num", obj.get("hhsl"));
                    rowdata.put("total", obj.get("caigouzonge"));
                    String baiwei = StringUtil.null2String(obj.get("baiwei"));
                    String lvzhi = StringUtil.null2String(obj.get("lvzhi"));
                    String hhlx = StringUtil.null2String(obj.get("hhlx"));

                    if (hhlx.equals("402881934060ce6c014060ee39760005")) hhlx = "报损";
                    else if (hhlx.equals("402881934060ce6c014060ee63250006")) hhlx = "代养";
                    else if (hhlx.equals("402881934060ce6c014060ee80ab0007")) hhlx = "基地";
                    else if (hhlx.equals("402881ee4278282501427983d20904c3")) hhlx = "加花";
                    else if (hhlx.equals("402881ee42b153ca0142da4e563313a9")) hhlx = "减花";
                    else if (hhlx.equals("402881ee4278282501427985399104c4")) hhlx = "赠花";
                    else if (hhlx.equals("402881ee4278282501427985787404c5")) hhlx = "售花";
                    else if (hhlx.equals("402881ee43226cb201435637c4f131ea")) hhlx = "调花";
                    else hhlx = "错误类型";
                    rowdata.put("type", hhlx);
                    String place1 = "";
                    String place2 = "";
                    String place3 = "";
                    try {
                        Map place3map = (Map) jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc,xianjia from ut_plantinfo where processid=?", new Object[]{lvzhi});
                        place3 += StringUtil.null2String(place3map.get("chmc"));
                        if (action.equals("getpurchaselist")) {
                            String xianjia = StringUtil.null2String(place3map.get("xianjia"));
                            rowdata.put("maxprice", xianjia);
                        }
                        Map place3map1 = (Map) jdbcDao.queryForMap("select bwsm from ut_postplant where processid=?", new Object[]{obj.get("bwlzid")});
                        if (!action.equals("getpurchaselist"))
                            place3 += " " + StringUtil.null2String(place3map1.get("bwsm"));
                        place3 += " " + StringUtil.null2String(optionService.get(StringUtil.null2String(place3map.get("chdlmc"))).getObjname());
                        place3 += " " + StringUtil.null2String(optionService.get(StringUtil.null2String(place3map.get("guigexinghao"))).getObjname());

                    } catch (Exception e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    rowdata.put("place3", place3);
                    try {
                        Map place2map = (Map) jdbcDao.queryForMap("select suoshulouyu,lcmc from ut_floorinfo where processid=?", new Object[]{baiwei});
                        place2 = StringUtil.null2String(place2map.get("lcmc"));
                        String suoshulouyu = StringUtil.null2String(place2map.get("suoshulouyu"));
                        Map place1map = (Map) jdbcDao.queryForMap("select lcmc from ut_floorinfo where processid=?", new Object[]{suoshulouyu});
                        place1 = StringUtil.null2String(place1map.get("lcmc"));
                    } catch (Exception e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                    rowdata.put("place2", place2);
                    rowdata.put("place1", place1);

                    String price = StringUtil.null2String(obj.get("caigoujine"));
                    rowdata.put("price", price);
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("result", jsonlist);
                gzip(request, response, JSON.toJSONString(object));
            } else {
                Map object = new HashMap();
                response.getWriter().print(JSON.toJSONString(object));
            }
        }
        if (action.equals("send") ) {
            String louyuid = StringUtil.null2String(request.getParameter("louyuid"));

            String sql1 = "select processid,yzdj,hhlx,hhsl,lzsl,lvzhi,baiwei,tobaiwei from ut_changeplant where hhzt='402881934061fddd0140622fe59a000a' and cgry=? and deleted=0 " +
                    "and caigoujine is not null and baiwei in(select id from ut_floorinfo where deleted=0 and suoshulouyu=?)";
            List<Map>  listes = jdbcDao.queryForList(sql1,new Object[]{Env.getRemoteUser().getId(), louyuid});
            if(listes!=null && listes.size()>0){
                for(int i =0 ;i<listes.size();i++) {
                    Map m = (Map)listes.get(i);
                    String processid = StringUtil.null2String(m.get("processid"));
                    String bwlzid = "";
                    String yzdj =  StringUtil.null2String(m.get("yzdj"),"0");
                    String lzsl =  StringUtil.null2String(m.get("lzsl"),"0");
                    String hhsl =  StringUtil.null2String(m.get("hhsl"),"0");
                    String bwid =  StringUtil.null2String(m.get("baiwei"));
                    String tobwid =  StringUtil.null2String(m.get("tobaiwei"));
                    String lzid =  StringUtil.null2String(m.get("lvzhi"));
                    OptionService optionService = Env.getBean("optionService");
                    Map lvzhidata = jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc from ut_plantinfo where processid=?", new Object[]{lzid});
                    String chdlmc =StringUtil.null2String(lvzhidata.get("chdlmc"));
                    if(m.get("hhlx").equals("402881ee4278282501427983d20904c3")){
                        String sq3 = "";
                        String sq4 = "";
                        String lvzhizd = "";
                        String thslzd = "";
                        String ggxhzd = "";
                        String mczd = "";
                        if(chdlmc.equals("402881923ff47408013ff54243eb0027")){
                            lvzhizd = "ut_postplant_huajia";
                            thslzd = "ut_postplant_hjsl";
                            ggxhzd = "ut_postplant_hjgg";
                            mczd =  "ut_postplant_hjmc";
                            sq3 = "select processid from ut_postplant where baiwei =? and huajia = ?";
                            sq4 = "update ut_postplant set hjsl = ? , yzdj = ? , yzzj = ? and where id =?";
                        }
                        else if (chdlmc.equals("402881923ff47408013ff54281b50028")){
                            lvzhizd = "ut_postplant_huapen";
                            thslzd = "ut_postplant_hpsl";
                            ggxhzd = "ut_postplant_hpgg";
                            mczd =  "ut_postplant_hpmc";
                            sq3 = "select processid,yzdj from ut_postplant where baiwei =? and huapen = ?";
                            sq4 = "update ut_postplant set hpsl = ?, yzdj = ? , yzzj = ? where id =?";
                        }
                        else{
                            lvzhizd = "ut_postplant_lvzhi";
                            thslzd = "ut_postplant_bfsl";
                            ggxhzd = "ut_postplant_ggxh";
                            mczd =  "ut_postplant_lzmc";
                            sq3 = "select processid,yzdj from ut_postplant where baiwei =? and lvzhi = ?";
                            sq4 = "update ut_postplant set bfsl = ? ,yzdj = ? ,yzzj = ? where id =?";
                        }

                        Float oldyzdj = 0f;
                        int oldbfsl = 0;
                        Float yzzj = 0f;
                        String newyzdj = "";//最终单价
                        List<Map> list =(List<Map>) jdbcDao.queryForList(sq3,new Object[]{bwid,lzid});
                        if(list!=null&&list.size()>0){  //摆位、绿植存在记录,更新摆放数量
                            Map postplant = list.get(0);
                            oldyzdj = Float.parseFloat(StringUtil.null2String(postplant.get("yzdj"),"0"));
                            oldbfsl = NumberUtil.getIntegerValue(lzsl);
                            int num = oldbfsl + NumberUtil.getIntegerValue(hhsl);
                            if(yzdj.equals("0")){
                                yzzj = oldyzdj*num;
                                newyzdj = String.valueOf(oldyzdj);
                            }
                            else{
                                yzzj = oldyzdj*oldbfsl + Float.parseFloat(yzdj)* NumberUtil.getIntegerValue(hhsl);
                                newyzdj = String.format("%.2f",yzzj/num);
                            }
                            jdbcDao.update(sq4,new Object[]{num,Float.parseFloat(newyzdj),yzzj,StringUtil.null2String(postplant.get("processid"))});
                        }
                        else{     //往ut_postplant表中插入一条新数据
                            yzzj = Float.parseFloat(yzdj)*NumberUtil.getIntegerValue(hhsl);
                            FormdataServiceImpl si = new FormdataServiceImpl();
                            Formdata formdata = new RequestInfo();
                            formdata.setOperator(Env.getRemoteUser().getId());
                            formdata.setTypeid("402881e640019e990140021f6805088c"); //摆位绿植目录id
                            Dataset ds = new Dataset();
                            List<Cell> mainlist = new ArrayList(); //主表单元格
                            Cell c1 =new Cell();
                            c1.setId("ut_postplant_baiwei");
                            c1.setValue(bwid);
                            Cell c2 =new Cell();
                            c2.setId(lvzhizd);
                            c2.setValue(lzid);
                            Cell c3 =new Cell();
                            c3.setId("ut_postplant_yzdj");
                            c3.setValue(yzdj);
                            Map baiweidata = jdbcDao.queryForMap("select lcmc from ut_floorinfo where processid=?", new Object[]{bwid});
                            Cell c4 =new Cell();
                            c4.setId("ut_postplant_bwsm");
                            c4.setValue(StringUtil.null2String(baiweidata.get("lcmc")));
                            Cell c5 =new Cell();
                            c5.setId(mczd);
                            c5.setValue(StringUtil.null2String(lvzhidata.get("chmc")));
                            Cell c6 =new Cell();
                            c6.setId(ggxhzd);
                            c6.setValue(StringUtil.null2String(optionService.get(StringUtil.null2String(lvzhidata.get("guigexinghao"))).getObjname()));
                            Cell c7 =new Cell();
                            c7.setId(thslzd);
                            c7.setValue(hhsl);
                            Cell c8 =new Cell();
                            c8.setId("ut_postplant_danwei");
                            c8.setValue(StringUtil.null2String(lvzhidata.get("zjldw")));
                            Cell c10 =new Cell();
                            c10.setId("ut_postplant_yzzj");
                            c10.setValue(String.valueOf(yzzj));
                            mainlist.add(c1);
                            mainlist.add(c2);
                            mainlist.add(c3);
                            mainlist.add(c4);
                            mainlist.add(c5);
                            mainlist.add(c6);
                            mainlist.add(c7);
                            mainlist.add(c8);
                            mainlist.add(c10);
                            ds.setMaintable(mainlist);
                            formdata.setData(ds);
                            si.createFormdata(formdata);



                            list =(List<Map>) jdbcDao.queryForList(sq3,new Object[]{bwid,lzid});
                            if(list!=null&&list.size()>0){
                                Map postplant = list.get(0);
                                bwlzid = StringUtil.null2String(postplant.get("processid"));
                                String sq = "update ut_changeplant set bwlzid = ? where id =?";
                                jdbcDao.update(sq,new Object[]{bwlzid,processid});
                            }
                        }
                    }
                   //减花
                    if(m.get("hhlx").equals("402881ee42b153ca0142da4e563313a9")){

                        String sql = "";
                        String sq = "";
                        if(chdlmc.equals("402881923ff47408013ff54243eb0027")){
                            sql = "update ut_postplant set hjsl =? , yzzj =? where baiwei =? and huajia =?";
                            sq = "select processid,yzdj from ut_postplant where baiwei =? and huajia = ?";
                        }
                        else if (chdlmc.equals("402881923ff47408013ff54281b50028")){
                            sql = "update ut_postplant set hpsl =? , yzzj =? where baiwei =? and huapen =?";
                            sq = "select processid,yzdj from ut_postplant where baiwei =? and huapen = ?";
                        }
                        else{
                            sql = "update ut_postplant set bfsl =? , yzzj =? where baiwei =? and lvzhi =?";
                            sq = "select processid,yzdj from ut_postplant where baiwei =? and lvzhi = ?";
                        }
                        List<Map> list =(List<Map>) jdbcDao.queryForList(sq,new Object[]{bwid,lzid});
                        if(list.size()>0){
                            int num = NumberUtil.getIntegerValue(lzsl) - NumberUtil.getIntegerValue(hhsl);
                            Map postplant = list.get(0);
                            Float oldyzdj = Float.parseFloat(StringUtil.null2String(postplant.get("yzdj"),"0"));
                            Float yzzj = 0f;
                            yzzj = oldyzdj*num;
                            jdbcDao.update(sql,new Object[]{num,yzzj,bwid,lzid});
                        }
                    }
                    //调花
                    if(m.get("hhlx").equals("402881ee43226cb201435637c4f131ea")){
                        String sq1 = "";
                        String sq2 = "";
                        String sq3 = "";
                        String sq4 = "";
                        String lvzhizd = "";
                        String thslzd = "";
                        String ggxhzd = "";
                        String mczd = "";
                        if(chdlmc.equals("402881923ff47408013ff54243eb0027")){
                            lvzhizd = "ut_postplant_huajia";
                            thslzd = "ut_postplant_hjsl";
                            ggxhzd = "ut_postplant_hjgg";
                            mczd =  "ut_postplant_hjmc";
                            sq1 = "select hjsl as bfsl,yzdj from ut_postplant where baiwei =? and huajia =?";
                            sq2 = "update ut_postplant set hjsl = ? , yzzj =? where baiwei =? and huajia =?";
                            sq3 = "select processid from ut_postplant where baiwei =? and huajia = ?";
                            sq4 = "update ut_postplant set hjsl = ? , yzdj =? , yzzj =? where id =?";
                        }
                        else if (chdlmc.equals("402881923ff47408013ff54281b50028")){
                            lvzhizd = "ut_postplant_huapen";
                            thslzd = "ut_postplant_hpsl";
                            ggxhzd = "ut_postplant_hpgg";
                            mczd =  "ut_postplant_hpmc";
                            sq1 = "select hpsl as bfsl,yzdj from ut_postplant where baiwei =? and huapen =?";
                            sq2 = "update ut_postplant set hpsl = ? , yzzj =? where baiwei =? and huapen =?";
                            sq3 = "select processid from ut_postplant where baiwei =? and huapen = ?";
                            sq4 = "update ut_postplant set hpsl = ? , yzdj =? , yzzj =? where id =?";
                        }
                        else{
                            lvzhizd = "ut_postplant_lvzhi";
                            thslzd = "ut_postplant_bfsl";
                            ggxhzd = "ut_postplant_ggxh";
                            mczd =  "ut_postplant_lzmc";
                            sq1 = "select bfsl,yzdj from ut_postplant where baiwei =? and lvzhi =?";
                            sq2 = "update ut_postplant set bfsl = ? , yzzj =? where baiwei =? and lvzhi =?";
                            sq3 = "select processid from ut_postplant where baiwei =? and lvzhi = ?";
                            sq4 = "update ut_postplant set bfsl = ? , yzdj =? , yzzj =? where id =?";
                        }
                        Float oldyzdj = 0f;
                        int oldbfsl = 0;
                        Float yzzj = 0f;
                        String newyzdj = "";//最终单价

                        //更新ut_postplant表中目的地（从）的摆放数量
                        List<Map> list1 = (List<Map>)jdbcDao.queryForList(sq1,new Object[]{bwid,lzid});
                        if(list1!=null&&list1.size()>0){
                            Map m1 = (Map)list1.get(0);
                            oldyzdj = Float.parseFloat(StringUtil.null2String(m1.get("yzdj"),"0"));           //原来的月租单价
                            oldbfsl = NumberUtil.getIntegerValue(StringUtil.null2String(m1.get("bfsl"),"0")); //原来的绿植数量
                        }
                        int num =oldbfsl - NumberUtil.getIntegerValue(hhsl);
                        yzzj = oldyzdj*num;
                        jdbcDao.update(sq2,new Object[]{num,yzzj,bwid,lzid});

                        //更新ut_postplant表中目的地（到）的摆放数量
                        List<Map> list =(List<Map>) jdbcDao.queryForList(sq3,new Object[]{tobwid,lzid});
                        if(list!=null&&list.size()>0){  //摆位、绿植存在记录,更新摆放数量
                            Map postplant = list.get(0);
                            List<Map> list2 = (List<Map>)jdbcDao.queryForList(sq1,new Object[]{tobwid,lzid});
                            if(list2!=null&&list2.size()>0){
                                Map m2 = (Map)list2.get(0);
                                oldyzdj = Float.parseFloat(StringUtil.null2String(m2.get("yzdj"),"0"));           //原来的月租单价
                                oldbfsl = NumberUtil.getIntegerValue(StringUtil.null2String(m2.get("bfsl"),"0")); //原来的绿植数量
                            }
                            num =oldbfsl + NumberUtil.getIntegerValue(hhsl);
                            if(yzdj.equals("0")){
                                newyzdj = String.valueOf(oldyzdj);
                                yzzj = oldyzdj*num;
                            }
                            else {
                                yzzj = oldyzdj*oldbfsl+Float.parseFloat(yzdj)*NumberUtil.getIntegerValue(hhsl);
                                newyzdj = String.format("%.2f",yzzj/num);
                            }
                            jdbcDao.update(sq4,new Object[]{num,Float.parseFloat(newyzdj),yzzj,StringUtil.null2String(postplant.get("processid"))});
                        }
                        else{   //摆位、绿植不存在记录，插入一条新的摆位绿植纪录
                            FormdataServiceImpl si2 = new FormdataServiceImpl();
                            Formdata formdata2 = new RequestInfo();
                            formdata2.setOperator(Env.getRemoteUser().getId());
                            formdata2.setTypeid("402881e640019e990140021f6805088c"); //摆位绿植目录id
                            Dataset ds2 = new Dataset();
                            yzzj = Float.parseFloat(yzdj)*NumberUtil.getIntegerValue(hhsl);
                            List<Cell> mainlist2 = new ArrayList(); //主表单元格
                            Cell c1 = new Cell();
                            c1.setId("ut_postplant_baiwei");
                            c1.setValue(tobwid);
                            Cell c2 = new Cell();
                            c2.setId(lvzhizd);
                            c2.setValue(lzid);
                            Cell c3 = new Cell();
                            c3.setId("ut_postplant_yzdj");
                            c3.setValue(yzdj);
                            Cell c4 = new Cell();
                            c4.setId("ut_postplant_bwsm");
                            Map baiweidata1 = jdbcDao.queryForMap("select lcmc from ut_floorinfo where processid=?", new Object[]{tobwid});
                            c4.setValue(StringUtil.null2String(baiweidata1.get("lcmc")));
                            Cell c5 = new Cell();
                            c5.setId(mczd);
                            c5.setValue(StringUtil.null2String(lvzhidata.get("chmc")));
                            Cell c6 = new Cell();
                            c6.setId(ggxhzd);
                            c6.setValue(StringUtil.null2String(optionService.get(StringUtil.null2String(lvzhidata.get("guigexinghao"))).getObjname()));
                            Cell c7 = new Cell();
                            c7.setId(thslzd);
                            c7.setValue(hhsl);
                            Cell c8 = new Cell();
                            c8.setId("ut_postplant_danwei");
                            c8.setValue(StringUtil.null2String(lvzhidata.get("zjldw")));
                            Cell c10 = new Cell();
                            c10.setId("ut_postplant_yzzj");
                            c10.setValue(String.valueOf(yzzj));
                            mainlist2.add(c1);
                            mainlist2.add(c2);
                            mainlist2.add(c3);
                            mainlist2.add(c4);
                            mainlist2.add(c5);
                            mainlist2.add(c6);
                            mainlist2.add(c7);
                            mainlist2.add(c8);
                            mainlist2.add(c10);
                            ds2.setMaintable(mainlist2);
                            formdata2.setData(ds2);
                            si2.createFormdata(formdata2);
                        }
                        list =(List<Map>) jdbcDao.queryForList(sq3,new Object[]{bwid,lzid});
                        if(list!=null&&list.size()>0){
                            Map postplant = list.get(0);
                            bwlzid = StringUtil.null2String(postplant.get("processid"));
                            String sq = "update ut_changeplant set bwlzid = ? where id =?";
                            jdbcDao.update(sq,new Object[]{bwlzid,processid});
                        }
                    }
                }
                StringBuffer sql = new StringBuffer("update ut_changeplant set hhzt='402881934061fddd014062306129000b' where  hhzt='402881934061fddd0140622fe59a000a' and cgry=? and deleted=0 and caigoujine is not null ");
                sql.append(" and baiwei in(select id from ut_floorinfo where deleted=0 and suoshulouyu=?)");
                jdbcDao.update(sql.toString(), new Object[]{Env.getRemoteUser().getId(), louyuid});//已送货完成
            }
                response.getWriter().print("success");

        }

        if (action.equals("assign") ) {
            String ids = StringUtil.null2String(request.getParameter("ids"));
            String humanid = StringUtil.null2String(request.getParameter("humanid"));
            StringBuffer sql = new StringBuffer("update ut_changeplant set cgry=?,hhzt='402881934061fddd0140622fe59a000a' where  hhzt='402881934061fddd0140622fbc2a0009' and deleted=0 and id in ("+StringUtil.formatMutiIDs(ids)+")");
            jdbcDao.update(sql.toString(), new Object[]{humanid});//分配采购
            //权限重构
            RightService rightService=Env.getBean("rightService");
            String[] ids_array=StringUtil.string2Array(ids,",");
            for (String processid : ids_array) {
                rightService.reCreateRightdetail(processid, "0", "ut_changeplant");
            }
            Message msg = new Message();
            msg.setMsgtitle("绿植采购");
            msg.setMsgcontent("您好！您新增了"+ids_array.length+"条绿植采购清单需要采购");
            msg.setMsgtype(Message.MESSAGE_TYPE_EMAIL);
            msg.setReceivers(humanid);
            msg.setSender(Env.getRemoteUser().getId());
            MessageProducer msgp =Env.getBean("messageProducer");
            msgp.send(msg);
            response.getWriter().print("success");

        }

        if(action.equals("getlzsl")){
            String lvzhi = StringUtil.null2String(request.getParameter("lvzhi"));
            String baiwei = StringUtil.null2String(request.getParameter("baiwei"));
            String sq1 = "";
            OptionService optionService = Env.getBean("optionService");
            Map lvzhidata = jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc,zjldw from ut_plantinfo where processid=?", new Object[]{lvzhi});
            String lzlx =StringUtil.null2String(lvzhidata.get("chdlmc"));
            if(lzlx.equals("402881923ff47408013ff54243eb0027")){
                sq1 = "select hjsl as bfsl from ut_postplant where baiwei =? and huajia =?";
            }
            else if (lzlx.equals("402881923ff47408013ff54281b50028")){
                sq1 = "select hpsl as bfsl from ut_postplant where baiwei =? and huapen =?";
            }
            else{
                sq1 = "select bfsl from ut_postplant where baiwei =? and lvzhi =?";
            }
            List list = jdbcDao.queryForList(sq1,new Object[]{baiwei,lvzhi});
            if(list!=null && list.size()>0){
                Map m = (Map)list.get(0);
                response.getWriter().print(StringUtil.null2String(m.get("bfsl"), "0"));
            }
        }
        if(action.equals("gethandsalelist")){
			 String processid = StringUtil.null2String(request.getParameter("processid"));
            String s = "select * from ut_changeplant where hhzt = '402881934061fddd0140622fe59a000a' and xgkh =? and cgry=? and deleted=0 and hhlx in ('402881e743387bc40143388b71590013','402881e743387bc40143388bacf60014') and caigoujine is not null";
            List<Map> sendlist1 = jdbcDao.queryForList(s, new Object[]{processid,Env.getRemoteUser().getId()});
            if(sendlist1.size() != 0) {
                List jsonlist = new ArrayList();
                for (Map obj : sendlist1) {
                    Map rowdata = new HashMap();
                    rowdata.put("processid", obj.get("id"));
                    rowdata.put("price", obj.get("caigoujine"));
                    rowdata.put("total", obj.get("caigouzonge"));
                    rowdata.put("num", obj.get("hhsl"));
                    rowdata.put("hhbz", obj.get("hhbz"));
                    String hhlx = "";
                    if(obj.get("hhlx").equals("402881ee4278282501427985399104c4"))  hhlx = "赠花";
                    else if(obj.get("hhlx").equals("402881ee4278282501427985787404c5"))  hhlx = "售花";
                    rowdata.put("type", hhlx);
                    Map lvzhidata1 = jdbcDao.queryForMap("select name from ut_customerinfo where processid=?", new Object[]{obj.get("xgkh")});
                    rowdata.put("place2", StringUtil.null2String(lvzhidata1.get("name")));

                     OptionService optionService = Env.getBean("optionService");
                    Map lvzhidata = jdbcDao.queryForMap("select guigexinghao,chmc,chdlmc,zjldw from ut_plantinfo where processid=?", new Object[]{obj.get("lvzhi")});
                    String chdlmc =optionService.get(StringUtil.null2String(lvzhidata.get("chdlmc"))).getObjname();
                    String chmc = StringUtil.null2String(lvzhidata.get("chmc"));
                    String guigexinghao = optionService.get(StringUtil.null2String(lvzhidata.get("guigexinghao"))).getObjname();
                    String place3 =chmc+ " "+guigexinghao ;
                    rowdata.put("place3", place3);
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("result", jsonlist);
                response.getWriter().print(JSON.toJSONString(object));
            }
        }
        if(action.equals("sendhandsale")){
            String processid = StringUtil.null2String(request.getParameter("processid"));
			String sql ="update ut_changeplant set hhzt='402881934061fddd014062308165000c' where  id in(select id from ut_changeplant where xgkh =? " +
                    "and deleted =0 and hhlx in ('402881ee4278282501427985399104c4','402881ee4278282501427985787404c5') and cgry=? and caigoujine is not null) ";
            jdbcDao.update(sql, new Object[]{processid,Env.getRemoteUser().getId()});
            response.getWriter().print("success");
        }
		if(action.equals("getcustomerlist")){//获取客户名单
            StringBuffer sql = new StringBuffer("select id,code,name from ut_customerinfo where  deleted=0 and id in( ");
            sql.append("select xgkh from ut_changeplant where  hhzt='402881934061fddd0140622fe59a000a' and hhlx in ('402881ee4278282501427985399104c4','402881ee4278282501427985787404c5') and cgry=? and deleted=0 and caigoujine is not null");
            sql.append(") ");

            List<Map> placelist = jdbcDao.queryForList(sql.toString(), new Object[]{Env.getRemoteUser().getId()});
            if (placelist.size() != 0) {
                List jsonlist = new ArrayList();
                for (Map obj : placelist) {
                    Map rowdata = new HashMap();
                    rowdata.put("id", obj.get("id"));
                    rowdata.put("processid", obj.get("id"));
                    rowdata.put("title", obj.get("name"));
                    jsonlist.add(rowdata);
                }
                Map object = new HashMap();
                object.put("result", jsonlist);
                gzip(request, response, JSON.toJSONString(object));
            } else {
                Map object = new HashMap();
                response.getWriter().print(JSON.toJSONString(object));
            }
        }





    }
}
